<?php
class Controller_Admin_Gonglue extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'gonglue';
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;

	        // 下发APP消息
	        if (! empty($post) )
	        {
	            $goods = new App_Gonglue($nid);
	            $goods->pushAppMessage();
	        }

        }
        else
        {
            $data = App_Gonglue::detail($nid);
            $this->_form_data = $data;
        }

        parent::action_update($nid);

    }

    public function action_detail ($nid)
    {
        $data = App_Gonglue::detail($nid);
        $this->_form_data = $data;
        $this->_form_data['image'] = explode("|", $data['image']);

        parent::action_detail($nid);
    }

    private function rebuild_post_data (& $post)
    {
        $post['uid']   = user::current_user_id();

        $goodsList = array();
        foreach ($post['goodsId'] as $index => $goodsId)
        {
            $title = Arr::get($post['goodsTitle'], $index);
            $desc  = Arr::get($post['goodsDesc'], $index);
            $imgs  = Arr::get($post['goodsImg'], $index, array());

            $goodsImgs = array();
            foreach ($imgs as $img)
            {
                $goodsImgs[] = array('goodsImg' => $img);
            }

            $goodsList[$index] = array(
                'goodsId' => $goodsId,
                'goodsTitle' => $title,
                'goodsDesc'  => $desc,
                'goodsImgs'  => $goodsImgs,
            );
        }

        $post['serializeGoodsList'] = serialize($goodsList);
    }

    public function list_filter_data (& $rows)
    {
    }

}
